import tkinter as tk
from typing import Callable

class CustomTitleBar(tk.Frame):
    """自定义标题栏组件，支持拖拽和按钮管理"""
    
    def __init__(self, parent, title: str = "自定义窗口", 
                 bg_color: str = "#2c3e50", text_color: str = "white",
                 height: int = 30, **kwargs):
        super().__init__(parent, height=height, bg=bg_color,** kwargs)
        
        # 配置属性
        self.parent = parent  # 父窗口引用
        self.title = title
        self.bg_color = bg_color
        self.text_color = text_color
        self.height = height
        
        # 拖拽相关变量
        self.dragging = False
        self.offset_x = 0
        self.offset_y = 0
        
        # 存储窗口控制按钮
        self.buttons = {}
        
        # 初始化UI
        self._init_ui()
        
        # 绑定拖拽事件
        self._bind_drag_events()
    
    def _init_ui(self) -> None:
        """初始化标题栏UI元素"""
        self.pack(fill=tk.X)
        
        # 标题文本
        self.title_label = tk.Label(
            self, 
            text=self.title, 
            bg=self.bg_color, 
            fg=self.text_color,
            font=("Arial", 10, "bold")
        )
        self.title_label.pack(side=tk.LEFT, padx=10)
        
        # 右侧按钮区域
        self.buttons_frame = tk.Frame(self, bg=self.bg_color)
        self.buttons_frame.pack(side=tk.RIGHT)
        
        # 添加默认关闭按钮
        self.add_button("close", "✕", "#e74c3c", self.parent.destroy)
    
    def add_button(self, name: str, text: str, bg_color: str, 
                  command: Callable, fg_color: str = "white") -> None:
        """
        添加按钮到标题栏
        
        :param name: 按钮名称，用于后续引用
        :param text: 按钮显示文本
        :param bg_color: 按钮背景色
        :param command: 按钮点击事件回调
        :param fg_color: 按钮前景色
        """
        btn = tk.Button(
            self.buttons_frame,
            text=text,
            bg=bg_color,
            fg=fg_color,
            borderwidth=0,
            width=3,
            height=1,
            command=command
        )
        btn.pack(side=tk.RIGHT, padx=1)
        self.buttons[name] = btn
    
    def _bind_drag_events(self) -> None:
        """绑定鼠标事件用于窗口拖拽"""
        self.bind("<ButtonPress-1>", self.start_drag)
        self.bind("<B1-Motion>", self.on_drag)
        self.bind("<ButtonRelease-1>", self.stop_drag)
        
        # 让标题文本也能响应拖拽事件
        self.title_label.bind("<ButtonPress-1>", self.start_drag)
        self.title_label.bind("<B1-Motion>", self.on_drag)
        self.title_label.bind("<ButtonRelease-1>", self.stop_drag)
    
    def start_drag(self, event) -> None:
        """开始拖拽窗口"""
        self.dragging = True
        self.offset_x = event.x
        self.offset_y = event.y
    
    def on_drag(self, event) -> None:
        """拖拽窗口过程"""
        if self.dragging:
            x = self.parent.winfo_x() + event.x - self.offset_x
            y = self.parent.winfo_y() + event.y - self.offset_y
            self.parent.geometry(f"+{x}+{y}")
    
    def stop_drag(self, event) -> None:
        """停止拖拽窗口"""
        self.dragging = False
    
    def set_title(self, title: str) -> None:
        """更新标题栏文本"""
        self.title = title
        self.title_label.config(text=title)
